package com.sunwayworld.common.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.hibernate.cfg.Configuration;

import com.sunwayworld.common.hibernate.HibernateSessionFactory;

public class ConnManager
 {
	static String JndiDSName = "";

	private static  Configuration cfg = null;

	private static String CONFIG_FILE_LOCATION = HibernateSessionFactory.CONFIG_FILE_LOCATION;

	public static Connection getConnection() {
		if (null == cfg) {
			cfg = new Configuration();
			cfg.configure(CONFIG_FILE_LOCATION);
		}

		Properties p = cfg.getProperties();
		String className = p.getProperty("hibernate.connection.driver_class");
		String url = p.getProperty("hibernate.connection.url");
		String userName = p.getProperty("hibernate.connection.username");
		String passWord = p.getProperty("hibernate.connection.password");
		Connection conn = null;
		try {
			System.out.println(url);
			System.out.println(className);
			Class.forName(className);
			conn = DriverManager.getConnection(url, userName, passWord);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Session session = HibernateSessionFactory.currentSession();
		// Connection conn = session.connection();
		return conn;
	}

	// public static Connection getConnection() throws SQLException {
	//
	// InitialContext initCtx = null;
	// try {
	// initCtx = new InitialContext();
	// DataSource ds = (javax.sql.DataSource) initCtx.lookup(JndiDSName);
	// return ds.getConnection();
	// } catch (NamingException ne) {
	// ne.printStackTrace();
	// throw new EJBException(ne);
	// } finally {
	// try {
	// if (initCtx != null)
	// initCtx.close();
	// } catch (Exception ne) {
	// ne.printStackTrace();
	// throw new EJBException(ne);
	// }
	// }
	// }

	public static DataSource getDataSource() {

		InitialContext initCtx = null;
		DataSource ds = null;
		try {
			initCtx = new InitialContext();
			ds = (javax.sql.DataSource) initCtx.lookup(JndiDSName);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return ds;
	}

	public static Connection getTxConnection() throws SQLException {

		InitialContext initCtx = null;
		try {
			initCtx = new InitialContext();
			DataSource ds = (javax.sql.DataSource) initCtx.lookup(JndiDSName);
			return ds.getConnection();
		} catch (NamingException ne) {
			ne.printStackTrace();
			throw new SQLException();
		} finally {
			try {
				if (initCtx != null)
					initCtx.close();
			} catch (Exception ne) {
				ne.printStackTrace();
				throw new SQLException();
			}
		}
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @param con
	 *            Connection
	 * @param stm
	 *            Statement
	 */
	public static void close(Connection con, Statement stm) {

		try {
			if (stm != null)
				stm.close();
			stm = null;
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			if (con != null)
				con.close();
			con = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @param con
	 *            Connection
	 * @param stm
	 *            Statement
	 * @param rs
	 *            ResultSet
	 */
	public static void close(Connection con, Statement stm, ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
			rs = null;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		try {
			if (stm != null)
				stm.close();
			stm = null;
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			if (con != null)
				con.close();
			con = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @param con
	 *            Connection
	 * @param ps
	 *            PreparedStatement
	 */
	public static void close(Connection con, PreparedStatement ps) {

		try {
			if (ps != null)
				ps.close();
			ps = null;
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			if (con != null)
				con.close();
			con = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @param con
	 *            Connection
	 * @param ps
	 *            PreparedStatement
	 * @param rs
	 *            ResultSet
	 */
	public static void close(Connection con, PreparedStatement ps, ResultSet rs) {

		try {
			if (rs != null)
				rs.close();
			rs = null;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		try {
			if (ps != null)
				ps.close();
			ps = null;
		} catch (Exception e) {
			e.printStackTrace();
		}

		try {
			if (con != null)
				con.close();
			con = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
